﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class ProvinceService : IProvinceService
    {
        /// <summary>
        /// 添加省份
        /// </summary>
        /// <param name="name">省份名 </param>
        /// <returns></returns>
        public long AddNew(string name)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Province> baseService = new BaseService<Province>(context);

                bool isExists = baseService.GetAll().Any(p => p.Name == name);

                if (isExists)
                {
                    throw new ArgumentException(string.Format($"省份：{name}已存在"));
                }

                Province province = new Province()
                {
                    Name = name
                };

                context.Provinces.Add(province);

                context.SaveChanges();

                return province.Id;
            }
        }


        /// <summary>
        /// 获取所有省份
        /// </summary>
        /// <returns></returns>
        public ProvinceDTO[] GetAll()
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Province> baseService = new BaseService<Province>(context);

                return baseService.GetAll().ToList()
                    .Select(p => ToDTO(p)).ToArray();
            }
        }

        /// <summary>
        /// 根据Id获取省份
        /// </summary>
        /// <param name="provinceId">省份Id</param>
        /// <returns></returns>
        public ProvinceDTO GetById(long provinceId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Province> baseService = new BaseService<Province>(context);

                var province = baseService.GetById(provinceId);

                return province == null ? null : ToDTO(province);
            }
        }

        /// <summary>
        /// 根据省份名获取省份
        /// </summary>
        /// <param name="provinceName"></param>
        /// <returns></returns>
        public ProvinceDTO GetByName(string provinceName)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Province> baseService = new BaseService<Province>(context);

                var province = baseService.GetAll().
                    SingleOrDefault(p => p.Name == provinceName);

                return province == null ? null : ToDTO(province);
            }
        }

        /// <summary>
        /// 软删除数据
        /// </summary>
        /// <param name="provinceId"></param>
        public void MarkDeleted(long provinceId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Province> baseService = new BaseService<Province>(context);

                Province province = baseService.GetById(provinceId);

                if (province == null)
                {
                    throw new ArgumentException(string.Format($"省份Id为：{provinceId}不存在"));
                }

                baseService.MakeDelete(provinceId);
            }
        }

        /// <summary>
        /// 更新省份
        /// </summary>
        /// <param name="provinceId">省份Id</param>
        /// <param name="name">省份名称</param>
        public void Update(long provinceId, string name)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Province> baseService = new BaseService<Province>(context);


                Province province = baseService.GetById(provinceId);

                if (province == null)
                {
                    throw new ArgumentException(string.Format($"省份Id为：{provinceId}不存在"));
                }

                province.Name = name;
                context.SaveChanges();
            }
        }

        private ProvinceDTO ToDTO(Province province)
        {
            return new ProvinceDTO
            {
                Id = province.Id,
                Name = province.Name,
                CreateDateTime = province.CreateDateTime
            };
        }
    }
}
